package com.kaili.basic.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.kaili.common.util.CommonUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.Version;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

/**
 * Created by rzh on 18-7-12.
 */
@Component
public class MdmAsyncUtils {

    private Logger logger = LoggerFactory.getLogger(MdmAsyncUtils.class);

    @Value("${spring.datasource.url}")
    private String url;

    @Value("${spring.datasource.username}")
    private String user;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.datasource.driverClassName}")
    private String driverClass;

    @Async
    public void createDbAndOthers(Map map) {
        try {
            executeSqlFile1(dataSource().getConnection(),map);
            executeSqlFile2(dataSource().getConnection(),map);
        } catch (IOException e) {
            logger.info("io异常{}",e);
        } catch (SQLException e) {
            logger.info("sql异常{}",e);
        } catch (TemplateException e) {
            logger.info("io异常{}",e);
        }
    }

    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(CommonUtil.druidDecrypt(password));
        return dataSource;
    }

    public void executeSqlFile1(Connection conn, Map map) throws IOException,SQLException,TemplateException {
        ScriptRunner runner = new ScriptRunner(conn);
        Resources.setCharset(Charset.forName("UTF-8"));
        runner.setLogWriter(null);
        runner.runScript(convertSql("database1.sql",map));
        runner.closeConnection();
        conn.close();
    }

    public void executeSqlFile2(Connection conn,Map map) throws IOException,SQLException,TemplateException {
        ScriptRunner runner = new ScriptRunner(conn);
        Resources.setCharset(Charset.forName("UTF-8"));
        runner.setLogWriter(null);
        runner.setDelimiter("DELIMITER ;");
        runner.runScript(convertSql("database2.sql",map));
        runner.closeConnection();
        conn.close();
    }

    public Reader convertSql(String resource, Map map) throws TemplateException,IOException{
        StringWriter result = new StringWriter();
        Template template = new Template("sql",Resources.getResourceAsReader(resource),new Configuration(new Version("2.3.0")));
        template.process(map,result);
        return new StringReader(result.toString());
    }
}
